package cn.zjxiliu.lib_login.ui.base;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import cn.zjxiliu.lib_core.utils.SoftKeyboardStateHelper;
import cn.zjxiliu.lib_core.widget.LoadingDialog;
import cn.zjxiliu.lib_login.R;


/**
 * Created by jessie on 2018/10/23.
 */

public abstract class AbsBaseViewImpl extends AppCompatActivity implements IBaseView {

    private LoadingDialog loadingDialog;


    private static final String TAG = "AbsBaseViewImpl";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }


    protected abstract void init();


    @Override
    public void isScreenLandscape(boolean isLandscape) {
        if(isLandscape){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }else{
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    @Override
    public void showLoadingDialog() {
        showLoadingDialog(getString(R.string.loading));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        hideLoadingDialog();
    }


    @Override
    public void showLoadingDialog(String msg) {
        if (loadingDialog != null) {
            if (loadingDialog.isShowing()) {
                return;
            }
        }
        loadingDialog = null;
        loadingDialog = new LoadingDialog.Builder(this)
                .setMessage(msg)
                .setCancelable(true)
                .setCancelOutside(true).create();

        loadingDialog.show();
    }

    @Override
    public void hideLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }



    @Override
    public void showToastMsg(String msg) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if(!this.isDestroyed()){
                Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        }
    }

    @Override
    public void showToastMsg(int msgId) {
        showToastMsg(getString(msgId));
    }

    @Override
    public void onBackPressed() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
            loadingDialog = null;
            cancelTask();
        }else{
            super.onBackPressed();
        }
    }

    protected abstract void cancelTask();


    @Override
    public Activity getCurrentActivity() {
        return this;
    }

    @Override
    public void finishSelf() {
        finish();
    }

    public void listenKeyboardLayout(final View root, final View scrollToView) {
        SoftKeyboardStateHelper keyboardStateHelper = new SoftKeyboardStateHelper(root);
        keyboardStateHelper.addSoftKeyboardStateListener(new SoftKeyboardStateHelper.SoftKeyboardStateListener() {
            @Override
            public void onSoftKeyboardOpened(int keyboardHeightInPx) {
                Rect rect = new Rect();
                // 获取root在窗体的可视区域
                root.getWindowVisibleDisplayFrame(rect);
                int[] location = new int[2];
                // 获取scrollToView在窗体的坐标
                scrollToView.getLocationInWindow(location);
                // 计算root滚动高度，使scrollToView在可见区域的底部
                int scrollHeight = (location[1] + scrollToView.getHeight()) - rect.bottom + 25;
                if(scrollHeight > 0){
                    root.scrollTo(0, scrollHeight);
                }
            }

            @Override
            public void onSoftKeyboardClosed() {
                // 键盘隐藏
                root.scrollTo(0, 0);
            }
        });
    }

}